<?php
namespace App\Services\Tool\Svg;
use App\Services\Tool\Svg\Svg;
use Log;

class SvgText extends Svg{
    private $id          = '';
    private $stroke      = 'none';
    private $fill        = 'none';
    private $font_size   = '50';
    private $font_weight = 'normal';
    private $text        = [];

    //默认参数
    private $default_font_size   = '50';
    private $default_font_family = 'Source Han Sans CN';
    private $default_font_weight = '50';
    private $default_fill        = '#000000';
    private $default_line_height = '50';
    private $default_line_margin = '1';


    public function id_set(string $id){
        $this->id = $id ? $id : time().'';
    }

    public function font_size_set(string $str){
        $this->font_size = $str;
    }

    public function font_weight_set(string $str){
        $this->font_weight = $str;
    }

    public function text_set(array $arr){
        if(count($arr) > 0){
            $this->text = $this->array_to_array($arr);
            return true;
        }
        return false;
    }

    private function get_text_str(){
        $str = '<text id="' . $this->id .
               '" stroke="' . $this->stroke .
               '" fill="' . $this->fill .
               '" font-size="' . $this->font_size .
               '" font-weight="' . $this->font_weight . '">';

        $str .= $this->get_tspan_str();

        $str .= '</text>';
        return $str;
    }

    private function get_tspan_str(){
        $str = '';
        $x   = 0;
        $y   = 0;
        foreach($this->text as $one){
            $str .= '<tspan ';
            if(isset($one['style'])){
                if($one['is_return']){
                    $x = 0;
                    $y += $one['line_height'] + $one['line_margin'];
                    $one['style']['x'] = 0;
                }else{
                    $x = $x == 0 ? $one['style']['font-width'] : $x;
                    $one['style']['x'] = $x;
                    unset($one['style']['dx']);
                    // unset($one['style']['x']);
                }

                // $one['style']['x'] = $x;
                $one['style']['y'] = $y == 0 ? $one['line_height'] + $one['line_margin'] : $y;
                $y = $one['style']['y'];
                foreach($one['style'] as $k=>$v){
                    if($k == 'font-size')
                        $str .= $k . '= "' . $v . 'px" ';
                    else
                        $str .= $k . '= "' . $v . '" ';
                }

                $x += $one['style']['font-width'];

            }
            $str .= '>' . $one['str'] . '</tspan>';
        }
        return $str;
    }

    public function get_all(){
        $str = $this->get_svg_body();
        $str .= '<svg width="' . $this->svg_width .
                '" height="' . $this->svg_height .
                '" viewBox="' . $this->view_box_get() .
                '" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">';
        $str .= $this->desc_get();
        $str .= $this->get_text_str();
        $str .= '</svg>';
        return $str;
    }

    /**
     * Tool
     */
    private function array_to_array(array $input){
        $all = [];
        foreach($input as $ar){
            if(!isset($ar['char']) || $ar['char'] == '') continue;
            $arr['str']                  = isset($ar['char']) ? $ar['char'] : '';
            $arr['style']['x']           = isset($ar['x']) ? $ar['x'] : '';
            $arr['style']['y']           = isset($ar['y']) ? $ar['y'] : '';
            $arr['style']['font-family'] = isset($ar['font_family']) ? $ar['font_family'] : $this->default_font_family;
            $arr['style']['font-size']   = isset($ar['font_size']) ? $ar['font_size'] : $this->default_font_size;
            $arr['style']['font-weight'] = isset($ar['font_weight']) ? $ar['font_weight'] : $this->default_font_weight

            $arr['style']['font-width']  = isset($ar['font_width']) ? $ar['font_width'] : 100;
            $arr['style']['fill']        = isset($ar['fill']) ? $ar['fill'] : $this->default_fill;
            $arr['is_return']            = isset($ar['is_return']) ? $ar['is_return'] : 0;
            $arr['line_height']          = isset($ar['line_height']) ? $ar['line_height'] : $this->default_line_height;
            $arr['line_margin']          = isset($ar['line_margin']) ? $ar['line_margin'] : $this->default_line_margin;
            $arr['letter-spacing']       = isset($ar['letter_spacing']) ? $ar['letter_spacing'] : 0;
            // $arr['style']['dx']          = $arr['is_return'] ? 0 : $arr['letter-spacing'];
            $all[] = $arr;
        }
        return $all;
    }

    private static function get_font_info($font_size, $font_path, $char, $font_angele = 0){
        $result = imagettfbbox($font_size, $font_angele, $font_path, $char);Log::info($result);
        $width  = abs($result[4] - $result[0]);
        $height = abs($result[5] - $result[1]);
        return ['width' => $width, 'height' => $height];
    }
}
